<!DOCTYPE html>
<html>
<head>
    <title>注册</title>
    <script src="js/jquery-3.2.1.min.js"></script>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<div class="container">
    <form class="login-form">
        <input name="mark" value="addUser" type="hidden" />
        <p><input type="text" placeholder="用户名" id="username"/></p>
        <p><input type="password" placeholder="密码" id="password"/></p>
        <p><input type="text" placeholder="性别" id="sex"/></p>
        <p><input type="text" placeholder="地址" id="phone"/></p>
        <p><input type="text" placeholder="手机号" id="address"/></p>
        <p><button type="button" id="register">注册</button></p>
    </form>
</div>

<script>
    $("#register").click(function() {
        //     获取输入框的值
        var username = $("#username").val();
        var password  = $("#password").val();
        var sex  = $("#sex").val();
        var phone  = $("#phone").val();
        var address  = $("#address").val();
        $.ajax({
            url: "userServlet",
            type: "post",
            data:{"name":username,"password":password,"sex":sex,"phone":phone,"address":address,"mark":"addUser"},
            dataType:"text",
            success:function (data){
                var jsonData=JSON.parse(data);
                if (jsonData==1){
                    window.location.href="login.html"
                }else {
                    alert("注册失败，请重新注册")
                    window.location.href="register.html"
                }
            },
            error:function (){
                alert("请求失败")
            }
        })

    });
</script>
</body>
</html>